package com.lamer.console.demo;

import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.HashSet;

public class Test {

    public static void main(String[] args) throws Exception{
        Example lamer = Example.builder().foo(1).bar("lamer").build();
        System.out.println(lamer);
        HashSet<String> files = mapperScan();
        for(String file: files){
            Class<?> aClass = Class.forName(file);
            if(aClass.isAnnotationPresent(IComponent.class)){
                IContainer.putBean(file, aClass.newInstance());
            }

            Method[] methods = aClass.getDeclaredMethods();
            for(Method method: methods){
                if(method.isAnnotationPresent(IBean.class)){
                    String beanName = method.getAnnotation(IBean.class).value();
                    IContainer.putBean(beanName, method.invoke(IContainer.getBean(file)));
                }
            }
        }
        


    }

    public static HashSet<String> mapperScan(){
        HashSet<String> set = new HashSet<>();
        String pac = "com.lamer.console.demo";
        URL resource = Test.class.getResource("/" + pac.replaceAll("\\.", "/"));
        File dir = new File(resource.getFile());
        for(File file: dir.listFiles()){
            if(file.isFile()){
                set.add(file.getName());
            }
        }
        return set;
    }
}
